
// 394.字符串解码
class Solution {
public:
    string decodeString(string s) {
        // 模拟栈来实现
        int n = s.size(), r = 0;
        stack<string> st;
        while (r < n) {
        if(isdigit(s[r])) {
            cout << s[r] << " ";
            int num = 0;
            // 提取数字
            while (r < n && isdigit(s[r]))
                num = num * 10 + s[r++] - '0';
            st.push(to_string(num));
            cout << num << endl;
            continue;
        }
        else if(s[r] == ']') {
            string tmp , instr;
            while (st.size() && st.top() != "[") {
                tmp += st.top();
                st.pop();
            }
            st.pop();
            int num = stoi(st.top());
            st.pop();
            for (int i = 0; i < num; i++) instr += tmp;
            st.push(instr);
        }
        else st.push({s[r]});
        r++;
    }
    string ret;
    while (st.size()) {
        ret += st.top();
        st.pop();
    }
    reverse(ret.begin(), ret.end());
    return ret;
}
}
;